# @Filename:    dataframe
# @Author:      王佳伟
# @Time:        2025-07-15 15:02
# @Describe:    DataFrame
import pandas as pd

# 创建空的DataFrame对象
# df = pd.DataFrame()
# print(df)

# 列表创建DataFrame对象
# 单一列表创建
# data = [1,2,3,4,5]
# df = pd.DataFrame(data)
# print(df)

# 嵌套列表创建
# data = [['Alex',10], ['Bob',12], ['Clarke',13]]
# df = pd.DataFrame(data, columns=['Name','Age'])
# print(df)

# 指定数值元素的数据类型为float
# data = [['Alex',10], ['Bob',12], ['Clarke',13]]
# df = pd.DataFrame(data, columns=['Name', 'Age']).astype({'Age':float})
# print(df)

# 字典嵌套列表创建
# data = {'Name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'Age': [28, 34, 29, 42]}
# df = pd.DataFrame(data)
# print(df)

# df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
# print(df)

# 列表嵌套字典创建DataFrame对象
# data = [{'a':1,'b':2}, {'a':5,'b':10,'c':20}]
# df = pd.DataFrame(data)
# df = pd.DataFrame(data, index=['first','second'])
# print(df)

# 使用字典嵌套列表以及行、列索引表创建一个 DataFrame 对象
# data = [{'a':1, 'b':2}, {'a':5, 'b':10, 'c':20}]
# df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b'])
# df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
# print(df1)
# print(df2)

# Series创建DataFrame对象
# d = {'one': pd.Series([1,2,3], index=['a','b','c']),
#      'two': pd.Series([1,2,3,4], index=['a','b','c','d'])}
# df = pd.DataFrame(d)
# print(df)

# 列索引操作DataFrame
# 列索引选取数据列
# d = {'one': pd.Series([1,2,3], index=['a','b','c']),
#      'two': pd.Series([1,2,3,4], index=['a','b','c','d'])}
# df = pd.DataFrame(d)
# print(df['one'])

# 列索引添加数据列
# d = {'one': pd.Series([1,2,3], index=['a','b','c']),
#      'two': pd.Series([1,2,3,4], index=['a','b','c','d'])}
# df = pd.DataFrame(d)
# 使用df['列']=值，插入新的数据列
# df['three'] = pd.Series([10,20,30], index=['a','b','c'])
# print(df)
# 将已经存在的数据列做相加运算
# df['four'] = df['one'] + df['three']
# print(df)

# 用insert()方法插入新的列
# info = [['Jack',18],['Helen',19],['John',17]]
# df = pd.DataFrame(info, columns=['name', 'age'])
# print(df)
# 注意是column参数
# 数值1代表插入到columns列表的索引位置
# df.insert(1,column='score',value=[91,90,75])
# print(df)

# 列索引删除数据列
# d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#      'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
#      'three': pd.Series([10, 20, 30], index=['a', 'b', 'c'])}
# df = pd.DataFrame(d)
# print("our dataframe is:")
# print(df)
# 使用del删除
# del df['one']
# print(df)
# 使用pop方法删除
# df.pop('two')
# print(df)

# 行索引操作DataFrame
# 标签索引选取
# d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#      'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
# df = pd.DataFrame(d)
# print(df.loc['b'])

# 整数索引选取
# d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#      'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
# df = pd.DataFrame(d)
# print(df)
# print(df.iloc[2])

# 切片操作多行选取
# d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#      'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
# df = pd.DataFrame(d)
# print(df)
# 左闭右开
# print(df[2:4])

# 添加数据行
# df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
# df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
# df = df._append(df2)
# print(df)

# 删除数据行
# df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
# df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
# df = df._append(df2)
# print(df)
# df = df.drop(0)
# print(df)